import Vue from 'vue'
import Vuex from 'vuex'

//安装插件
Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		// 1.系统信息
		systemInfo: {
			// "brand": "Apple",
			// "model": "iPhoneSimulator",
			// "pixelRatio": 3,
			// "screenWidth": 375,
			// "screenHeight": 812,
			// "windowWidth": 375,
			// "windowHeight": 778,
			// "statusBarHeight": 44,
			// "language": "en",
			// "system": "11.0.1",
			// "version": "1.9.9.80416",
			// "fontSizeSetting": "",
			// "platform": "ios",
			// "SDKVersion": "",
			// "windowTop": 0,
			// "windowBottom": 0,
			// "safeArea": {
			// 	"left": 0,
			// 	"right": 375,
			// 	"top": 44,
			// 	"bottom": 778,
			// 	"width": 375,
			// 	"height": 734
			// },
			// "safeAreaInsets": {
			// 	"top": 44,
			// 	"right": 0,
			// 	"bottom": 0,
			// 	"left": 0
			// }
		},
		//2.登录状态
		loginStatus: false,
		// 3.登录信息
		token: '',
		user: {
			// "id": 5,
			// "mobile": "186383158792",
			// "invite_code": "xx157",
		},

	},
	mutations: {
		// 1.更改手机基本参数  px转rpx
		changeSystemInfo(state, oldSystemInfo) {

			var ratio = 750.0 / oldSystemInfo.screenWidth;

			var temBottom = oldSystemInfo.screenHeight - oldSystemInfo.statusBarHeight - oldSystemInfo.safeArea
				.height;
			var temAllNavHeight = 0;

			if (oldSystemInfo.platform == 'android') {
				temAllNavHeight = (oldSystemInfo.safeArea.top + 50)*ratio;
			} else {
				temAllNavHeight = (oldSystemInfo.safeArea.top + 45)*ratio;
			};

			var temHeight = ratio * oldSystemInfo.safeArea.height;
			var temNavBarTop = ratio * oldSystemInfo.safeArea.top
			var newSystemInfo = {
				"model": oldSystemInfo.model,
				"pixelRatio": oldSystemInfo.pixelRatio,
				"safeAreaHeight": temHeight,
				"windowWidth": 750,
				"statusBarHeight": ratio * oldSystemInfo.statusBarHeight,
				"language": oldSystemInfo.language,
				"system": oldSystemInfo.system,
				"platform": oldSystemInfo.platform,
				"safeAreaBottom": ratio * temBottom,
				"top": temNavBarTop,
				'allNavHeight':temAllNavHeight
			}

			state.systemInfo = newSystemInfo;

		},


		//2.登录
		login(state, payload) {
			// 改变登录状态,存储token, 存储用户信息
			state.loginStatus = true
			state.token = payload.token
			state.user = payload.user
			// state.location = payload.location
			console.log(state);

			//持久化存储
			uni.setStorageSync('user', payload.user)
			uni.setStorageSync('token', payload.token)

			// uni.setStorageSync('location', JSON.stringify(payload.location))
		},

		// 3.退出登录
		loginOut(state) {
			state.loginStatus = false
			state.user = {}
			state.location = {}
			state.token = ''
			uni.clearStorageSync();
			// uni.removeStorageSync('user')
			// uni.removeStorageSync('token')
			// // 主要是针对与 小程序端 删除 手机号授权记录
			// uni.removeStorageSync('UserAuthorTelNum');
			// uni.removeStorageSync('UserInfoAuthor');


		},

		//启动APP时初 始化用户信息
		initUser(state) {
			let user = uni.getStorageSync('user')
			let token = uni.getStorageSync('token')
			if (user) {
				state.loginStatus = true
				state.user = user
			}

			if (token) {
				state.token = uni.getStorageSync('token')

			}
		},
		updateUser(state, user) {
			console.log(user)
			// 改变存储用户信息
			state.user = user
			//持久化存储
			uni.setStorageSync('user', user)
		},
		// 修改用户信息(手机号,邮箱,密码,头像)
		editUserInfo(state, {
			key,
			value
		}) {
			state.user[key] = value
			uni.setStorageSync('user', state.user);
		},

	},
	actions: {


	}
})


export default store
